package net.wofly.sale.commodity.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import net.wofly.common.util.Identities;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.commodity.domain.Commodity;
import net.wofly.sale.commodity.domain.CommodityChannel;
import net.wofly.sale.commodity.domain.OrderTypeChannelMapping;
import net.wofly.sale.commodity.repository.CommodityChannelRepository;
import net.wofly.sale.commodity.service.ICommodityChannelService;
import net.wofly.sale.commodity.service.ICommodityService;
import net.wofly.sale.commodity.service.IOrderTypeChannelMappingService;
import net.wofly.sale.commodity.util.Channel;

@Service("iCommodityChannelService")
@Transactional
public class CommodityChannelServiceImpl implements ICommodityChannelService {

	@Autowired
	CommodityChannelRepository commodityChannelRepository;
	@Autowired
	ICommodityService iCommodityService ;
	
	@Autowired
	IOrderTypeChannelMappingService  iOrderTypeChannelMappingService ;
	

	@Override
	public CommodityChannel save(CommodityChannel entity) {
		return commodityChannelRepository.save(entity);
	}

	@Override
	public void delete(CommodityChannel entity) {
		commodityChannelRepository.delete(entity);
	}

	@Override
	public void delete(String key) {
		CommodityChannel commodityChannel = commodityChannelRepository.findOne(key);
		if (commodityChannel != null) {
			commodityChannel.setStatus(0);
			commodityChannel.setModifyDate(System.currentTimeMillis());
			commodityChannelRepository.save(commodityChannel);
		}
	}

	@Override
	public CommodityChannel findOne(String key) {
		return commodityChannelRepository.findOne(key);
	}

	@Override
	public List<CommodityChannel> save(List<CommodityChannel> entityList) {
		return (List<CommodityChannel>) commodityChannelRepository.save(entityList);
	}

	@Override
	public ReturnBean<CommodityChannel> addCommodityChannel(CommodityChannel commodityChannel) {
		List<CommodityChannel> commodityChannelList = commodityChannelRepository.findByCommodityChannelCode(commodityChannel.getCommodityChannelCode());
		if (commodityChannelList != null && commodityChannelList.size() > 0){
			return new ReturnBean<>(-2, "商品渠道编码重复", commodityChannel);
		}
		commodityChannel.setCommodityChannelID(Identities.uuid2());
		commodityChannel.setModifyDate(System.currentTimeMillis());
		commodityChannel.setStatus(1);
		return new ReturnBean<>(0, "添加商品渠道及价格设置成功", commodityChannelRepository.save(commodityChannel));
	}

	@Override
	public ReturnBean<List<CommodityChannel>> addCommodityChannels(List<CommodityChannel> commodityChannels) {
		for (CommodityChannel commodityChannel : commodityChannels) {
			List<CommodityChannel> commodityChannelList = commodityChannelRepository.findByCommodityChannelCode(commodityChannel.getCommodityChannelCode());
			if (commodityChannelList != null && commodityChannelList.size() > 0){
				return new ReturnBean<>(-2, "商品渠道编码重复", commodityChannelList);
			}
		}
		return new ReturnBean<>(0, "添加商品渠道及价格设置成功", (List<CommodityChannel>) commodityChannelRepository.save(commodityChannels));
	}

	@Override
	public ReturnBean<CommodityChannel> updateCommodityChannel(CommodityChannel commodityChannel) {
		CommodityChannel dbCommodityChannel = commodityChannelRepository.findOne(commodityChannel.getCommodityChannelID());
		if (dbCommodityChannel == null) {
			return new ReturnBean<>(-2, "要修改商品渠道及价格信息不存在！", commodityChannel);
		}
		List<CommodityChannel> commodityChannelList = commodityChannelRepository.findByCommodityChannelCode(commodityChannel.getCommodityChannelCode());
		if (commodityChannelList != null && commodityChannelList.size() > 0){
			for (CommodityChannel entity : commodityChannelList) {
				if (!entity.getCommodityChannelID().equals(dbCommodityChannel.getCommodityChannelID())) {
					return new ReturnBean<>(-3, "商品渠道编码重复", commodityChannel);
				}
			}
		}

		commodityChannel.setModifyDate(System.currentTimeMillis());
		return new ReturnBean<>(0, "修改商品渠道及价格设置成功", commodityChannelRepository.save(commodityChannel));
	}
	
	@Override
	public ReturnBean<List<CommodityChannel>> updateCommodityChannels(Commodity commodity) {
		Map<String ,CommodityChannel> commodityChannelMap = new HashMap<>() ;
		
		List<CommodityChannel> commodityChannels = commodity.getCommodityChannelList() ;
		for (CommodityChannel commodityChannel : commodityChannels) {
			commodityChannelMap.put(commodityChannel.getChannel(), commodityChannel) ;
			
			commodityChannel.setCommodityID(commodity.getCommodityID());
			commodityChannel.setModifyDate(System.currentTimeMillis());
			commodityChannel.setStatus(1);
		}
		
		CommodityChannel tmpCommodityChannel ;
		List<CommodityChannel> dbCommodityChannels = commodityChannelRepository.findByCommodityID(commodity.getCommodityID()) ;
		for (CommodityChannel dbCommodityChannel : dbCommodityChannels) {
			
			tmpCommodityChannel = commodityChannelMap.get(dbCommodityChannel.getChannel()) ;
			if (null != tmpCommodityChannel) {
				dbCommodityChannel.setCommodityChannelPrise( tmpCommodityChannel.getCommodityChannelPrise());
			}
			
			
			dbCommodityChannel.setModifyDate(System.currentTimeMillis());
			dbCommodityChannel.setStatus(1);
		}
		
		return new ReturnBean<>(0, "添加商品渠道及价格设置成功", (List<CommodityChannel>) commodityChannelRepository.save(dbCommodityChannels));
	}

	@Override
	public List<CommodityChannel> findByCommodityID(String commodityID) {
		List<CommodityChannel> commodityChannels = commodityChannelRepository.findByCommodityID(commodityID);
		
		for(CommodityChannel commodityChannel:commodityChannels) {
			commodityChannel.setChannelStr(Channel.getName(Integer.parseInt(commodityChannel.getChannel())));
		}
		
		return commodityChannels;
	}

	@Override
	public CommodityChannel findByCommodityIDAndChannel(String commodityID, String channel) {
		List<CommodityChannel> commodityChannels = commodityChannelRepository.findByCommodityIDAndChannelAndStatus(commodityID, channel, 1);
		if (commodityChannels.size() > 0) {
			return commodityChannels.get(0);
		}
		return null;
	}

	@Override
	public ReturnBean<List<CommodityChannel>> getAllCommodityChannelList() {
		List<Order> orders=new ArrayList<>();
		orders.add(new Order(Direction.ASC, "commodityID"));
		orders.add(new Order(Direction.ASC, "channel"));
		
		List<CommodityChannel> list = commodityChannelRepository.getAllByStatus(1 ,new Sort(orders));
		
		return new ReturnBean<>(0, "获取所有渠道商品成功",list) ;
	}

	@Override
	public String findCommodityNameByCommodityChannelID(String commodityChannelID) {
		return commodityChannelRepository.findCommodityNameByCommodityChannelID(commodityChannelID);
	}

	@Override
	public void delByCommodityID(String commodityID) {
		List<CommodityChannel> commodityChannels = commodityChannelRepository.findByCommodityID(commodityID) ;
		
		for(CommodityChannel commodityChannel:commodityChannels) {
			commodityChannel.setStatus(0);
		}
		commodityChannelRepository.save(commodityChannels) ;
	}

	@Override
	public ReturnBean<List<CommodityChannel>> getCommodityChannelByOrderType(String orderType) {
		if ("-1".equals(orderType)) {
			return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "获取订单类型对应渠道商品列表成功",null) ;
		}
		
		OrderTypeChannelMapping orderTypeChannelMapping = iOrderTypeChannelMappingService.findByOrderType(orderType) ;
		
		if (null == orderTypeChannelMapping) {
			return new ReturnBean<>(-1, "订单类型未配置对应的渠道") ;
		}
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "获取订单类型对应渠道商品列表成功",commodityChannelRepository.findByChannelAndStatus(orderTypeChannelMapping.getChannel() ,1) ) ;
	}
}
